package main

import (
	"math"
	"strconv"
)

/*
 * @lc app=leetcode.cn id=8 lang=golang
 *
 * [8] 字符串转换整数 (atoi)
 */

// @lc code=start
func myAtoi(s string) int {
	if len(s) == 0 {
		return 0
	}
	var symbol uint8 = 43
	var num []byte
	for i, _ := range s {
		if s[i] == 32 {
			continue
		}
		s=s[i:]
		break
	}
	if s[0] == 45 || s[0] == 43 {
		symbol = s[0]
		s = s[1:]
	}
	for i , _ := range s {
		if s[i] >=48 && s[i] <= 57 {
			num = append(num, s[i])
		} else {
			break
		}
	}
	if len(num) == 0 {
		return 0
	}
	n, _ := strconv.Atoi(string(num))
	if symbol == 45 {
		n = -n
	}
	ne := math.Pow(-2, 31)
	if float64(n) < ne {
		return int(ne)
	}
	pv := math.Pow(2, 31)-1
	if float64(n) > pv {
		return int(pv)
	}
	return n
}
// @lc code=end

